package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.CartItemConverter;
import com.cskaoyan.shopping.dto.CheckAllItemRequest;
import com.cskaoyan.shopping.dto.CheckAllItemResponse;
import com.cskaoyan.shopping.dto.DeleteCheckedItemRequest;
import com.cskaoyan.shopping.dto.DeleteCheckedItemResposne;
import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ycc
 * @date 2022/07/29 16:27:32
 */
@RestController
public class ItemsController {
    @Autowired
    ICartService iCartService;

    @DeleteMapping("shopping/items/{id}")
    public ResponseData deleteCheckedItem(@PathVariable("id")Long id){
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(id);
        DeleteCheckedItemResposne resposne = iCartService.deleteCheckedItem(request);
        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(resposne.getCode())) {
            return new ResponseUtil().setData(resposne.getMsg());
        }
        return new ResponseUtil().setErrorMsg(resposne.getMsg());
    }

    @PutMapping("shopping/items")
    public ResponseData CheckedAllOrNone(@RequestBody CartForm cartForm){
        CheckAllItemRequest request = CartItemConverter.cartFormToTarget(cartForm, new CheckAllItemRequest());
        CheckAllItemResponse response = iCartService.checkAllCartItem(request);
        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
